package com.demo.sentinel.init;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.csp.sentinel.cluster.ClusterStateManager;
import com.alibaba.nacos.api.PropertyKeyConst;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 嵌入模式
 * @author cyj
 **/
@Configuration
public class EmbeddedClusterServerInitFunc implements InitializingBean {

    @Resource
    private NacosConfigProperties nacosConfigProperties;

    @Override
    public void afterPropertiesSet() {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfigProperties.getServerAddr());
        properties.put(PropertyKeyConst.NAMESPACE, nacosConfigProperties.getNamespace());

        ServerConfigUtil.init(properties);

        // 指定当前身份为 Token Server
        ClusterStateManager.applyState(ClusterStateManager.CLUSTER_SERVER);
    }
}
